package com.tool.write.simple;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.tool.write.entity.Person;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author niuyicong
 * @date 2024/10/18  18:25:51
 * @description 一个简单的写入 配置@ExcelProperty注解里面的index属性值
 */
public class SimpleWrite {
    public static void main(String[] args) {
        String fileName = "个人信息" + System.currentTimeMillis() + ".xlsx";
        // 写法一
        EasyExcel.write(fileName, Person.class)
                .sheet("模板")
                .doWrite(() -> {
                    return data();
                });
        // 写法二
        //EasyExcel.write(fileName, Person.class).sheet("模板").doWrite(data());
        // 写法三
//        try (ExcelWriter excelWriter = EasyExcel.write(fileName, Person.class).build()) {
//            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
//            excelWriter.write(data(), writeSheet);
//        }
    }

    public static List<Person> data(){
        List<Person> data = new ArrayList<>();
        Person person1 = new Person("niu",25,new Date(),"男");
        Person person2 = new Person("niuniu",26,new Date(),"男");
        Person person3 = new Person("niuniuniu",27,new Date(),"男");
        data.add(person1);
        data.add(person2);
        data.add(person3);
        return data;
    }
}
